OPTIONS LINESIZE=80;
TITLE 'ampdur';

/*
libname libB 'C:\Documents and Settings\user1\My Documents\bigfiles\ampdur';
libname libB 'C:\Documents and Settings\yoav\My Documents\school\BigFiles\ampdur';

libname libS 'C:\Documents and Settings\yoav\My Documents\My Dropbox\work1\AMP\ampdur\results';
libname libS 'C:\Documents and Settings\user1\My Documents\My Dropbox\work\research\att\measure\AMP\amp.studies\ampdur\results';

libname libS 'D:\Users\baranany\Dropbox\work\research\att\measure\AMP\amp.studies\ampdur\results';

*/
/*Learn about the session*/
data ssds; set libB.sessiontasks1500000 libB.sessiontasks1600000; subject = SESSION_ID; run;
/*proc freq data=ssds; tables TASK_ID_S; run;*/
/*proc freq data=ssds; tables TASK_URL_S; where session_date > '1Dec2007:20:45:00'dt; RUN;*/
proc freq data=ssds; tables TASK_ID_S; run;
PROC SORT DATA=ssds; BY subject TASK_ID_S; RUN;
PROC UNIVARIATE DATA=ssds NOPRINT;
VAR TASK_NUMBER;OUTPUT OUT=usession mean=mNum mode=tNum;BY subject TASK_ID_S;RUN;
PROC TRANSPOSE DATA=usession OUT=orderDS prefix=ord;VAR TASK_ID_S;ID tNum;BY subject;RUN;
data libB.orderDS; set orderDS;run;
/*proc freq data=orderDS; tables ord14; run;*/

/********************************************/


data ds; set libB.iats1500000 libB.iats1600000; run;
/***********/
/*READ AMPs*/
data ampraw; set ds; if index(task_name_s, "amp");run;
proc freq data=ampraw; tables trial_name_s; run;
DATA ampraw; SET ampraw;
	if block_name_s = "BLOCK0" then delete;

	prime = '123456789123456789'; prm = '123456';

	prime = substr(TRIAL_NAME_S, index(TRIAL_NAME_S, "[")+1, 
			index(TRIAL_NAME_S, ".") - index(TRIAL_NAME_S, "[") - 1);
	*set cond;
	if (index(prime, "ybwh") > 0) then rcprm = "wht";
	else if (index(prime, "ybbl") > 0) then rcprm = "blk";
	else if (index(prime, "Grey") > 0) then rcprm = "gry";

	prm = rcprm || gnprm;

	*set reponse;
	if TRIAL_RESPONSE_S eq "Pleasant" then plsnt = 1; 
	else if TRIAL_RESPONSE_S eq "Unpleasant" then plsnt = 0; 

	subject = SESSION_ID;

	if block_name_s="BLOCK1" then part=1;
	if block_name_s="BLOCK2" then part=2;

	if prime="ybblackyng1" then prime="ybblackyng0";
	if prime="ybwhiteyng1" then prime="ybwhiteyng0";
run;
proc freq data=ampraw; tables prime; run;
proc freq data=ampraw; tables trial_number; where part=2; run;

/*AMP*/
data libb.foramp; set ampraw; run;
/*score*/
/*
%include 'C:\Documents and Settings\yoav\My Documents\school\workn\work1\stasExamples\score\scoreMac.sas';
%include 'C:\Documents and Settings\yoav\My Documents\school\workn\work1\stasExamples\score\excludeMac.sas';
%include 'C:\Documents and Settings\yoav\My Documents\school\workn\work1\stasExamples\score\getsidexc.sas';
*/
%include 'C:\Documents and Settings\user1\My Documents\workn\work1\stasExamples\score\scoreMac.sas';
%include 'C:\Documents and Settings\user1\My Documents\workn\work1\stasExamples\score\excludeMac.sas';
%include 'C:\Documents and Settings\user1\My Documents\workn\work1\stasExamples\score\getsidexc.sas';
%ampScore(libIn=libb, libOut=libb, indata=foramp, outdata=amp, SUBJECT=session_id, TRIAL_COND=prm, 
trlResp=plsnt, parcelVar=part, prefix=amp, debug=1);
proc print data=libb.amp;run;

%include 'C:\Documents and Settings\user1\My Documents\workn\work1\stasExamples\score\scoreMac.sas';
%include 'C:\Documents and Settings\user1\My Documents\workn\work1\stasExamples\score\excludeMac.sas';
%include 'C:\Documents and Settings\user1\My Documents\workn\work1\stasExamples\score\getsidexc.sas';
%ampScore(libIn=libb, libOut=libb, indata=foramp, outdata=amps, SUBJECT=session_id, TRIAL_COND=prime, 
trlResp=plsnt, parcelVar=parts, prefix=amps, debug=1);
proc contents short data=libb.amps; run;


/*AMP response bias*/
PROC SORT DATA=ampraw; BY session_id block_number trial_number; RUN;
data forampex; set ampraw; sameRsp =  (trial_number ne 0) and (plsnt=lag(plsnt));run;
/*proc print data=forampex;var session_id block_number trial_number plsnt sameRsp; run;*/
PROC MEANS DATA=forampex n MEAN MAXDEC=2 fw=7 std stderr;
VAR sameRsp;CLASS subject; output out=ampAlt mean=sameRsp; RUN;
data ampAlt; set ampAlt; nAMPs = _freq_; keep subject nAMPs sameRsp;run;
proc print data=ampalt;run;
proc freq data=ampalt; tables namps; run;
data libb.ampalt; set ampalt;run;
/*IAT*/
/*race*/
data forir; set ds; where task_name_s in ("iwhtgood" "iwhtbad");
if index(BLOCK_PAIRING_DEFINITION_S, "Black People/Good Words,White People/Bad Words")>0 then cond = "bg";
else if index(BLOCK_PAIRING_DEFINITION_S, "White People/Good Words,Black People/Bad Words")>0 then cond = "wg";
else delete;
if block_number in (2 5) then part = 1;
else if block_number in (3 6) then part = 2;
err = input(TRIAL_ERROR,4.0);
run;
proc freq data=forir; tables cond; run;
proc freq data=forir; tables part; run;
data libB.forir; set forir; run;
%exclude(libIn=libb, libOut=libb, indata=forir, outdata=forir, sessionID=session_id, parcelVar=part, 
cndVar=cond, trlRT=trial_latency, trlErr=err, excludeErr=0, trlExc=trlExc, sidExc=IATsidExc1, prcExc=prcExc, 
minRT=300, maxRT=10000, sidErrRate=0.4, sidFastRate=0.4, sidSlowRate=0.4, sidFastSlowRate=0.4, 
sidFastSlowErrRate=0.4, sidNGoodTrials=40, prcErrRate=0.4, prcFastRate=0.4, 
prcSlowRate=0.4, prcFastSlowRate=0.4, prcFastSlowErrRate=0.4, prcNGoodTrials=15, debug=0);
/*save the sidexc*/
%getSIDExc(libb, libb, forir, excsidr, session_id, IATsidExc1);
proc contents short data=libb.excsidr;run;
proc freq data=libb.excsidr; tables IATsidExc1; run;
proc freq data=libb.excsidr; tables session_id; run;
%score(libIn=libb, libOut=libb, indata=forir, outdata=iatr, SUBJECT=session_id, TRIAL_COND=cond, 
TRIAL_LATENCY=trial_latency, parcelVar=part, trlExc=trlExc, prefix=iatr, debug=0);
data libb.iatr; set libb.iatr; 
iat_rw = mean(iatr_rwg, iatr_rbg); iat_d = (iatr_rbg-iatr_rwg)/iatr_s;
iat_d1 = (iatr_rbg1-iatr_rwg1)/iatr_s1;iat_d2 = (iatr_rbg2-iatr_rwg2)/iatr_s2;
run;
proc corr nosimple alpha data=libb.iatr;  var iat_d1 iat_d2;run;
/*ADD IAT ORDER CONDITION*/
data libb.iorder; set ds; 
if task_name_s in ("iwhtgood" "iwhtbad") and block_number = 0 and trial_number=0; 
iorder=task_name_s;subject=session_id;
keep subject iorder;run;


/*read explicit*/
proc freq data=libB.explicits15000000; tables QUESTIONNAIRE_NAME_S; run;
data eds; set libB.explicits1500000 libB.explicits1600000;
if index(QUESTIONNAIRE_NAME_S, "exp") or index(QUESTIONNAIRE_NAME_S, "post");
run;
proc freq data=eds; tables QUESTION_NAME_S;run;
data eExp; set eds;
subject=SESSION_ID; 
expR = INPUT(QUESTION_RESPONSE_S, 4.0); 
qst = QUESTION_NAME_S; qstr = QUESTIONNAIRE_NAME_S;
qstN = QUESTION_NUMBER;
keep subject TASK_NUMBER SESSION_ID qst qstr qstN expR; run;
proc sort data=eexp;by session_id;run;
PROC TRANSPOSE DATA=eexp OUT=exp;VAR expR;BY session_id;ID qst;RUN;
proc contents short data=exp;run;
PROC MEANS DATA=exp n MEAN MAXDEC=2 fw=7 std stderr;
VAR  eBlack eWhite infl inflBlk inflWht intent intentBlk intentWht
read sBlackM sBlackW sWhiteM sWhiteW;RUN;
data libb.exp; set exp; run;

/*Read duration*/
data startT; set ssds; if TASK_ID_S = "con"; startT = TASK_CREATION_DATE; run;
data endT; set ssds; if TASK_ID_S = "dbrf"; endT = TASK_CREATION_DATE; run;
PROC SORT DATA=startT; BY SESSION_ID; RUN;
PROC SORT DATA=endT; BY SESSION_ID; RUN;
data timeds; merge startT endT; by SESSION_ID; duration = endT - startT;run;
proc freq data=timeds; tables duration; run;
data libb.timeds; set timeds;run;

*****************************;
*add data from registration;
data demo; set libB.demos; run;
proc sort data=demo; by USER_ID;run;
proc transpose data=demo name=name out=demoDS; by USER_ID; var VALUE_S;id CHARACTERISTIC_S;
/*proc print data=demoDS;run;*/
data demoDS; set demoDS; 
format dPlt 2.;
dPlt = politicalid;
age = 2007-input(birthyear,4.0);
orgn = citizenship;
race='.....';
if raceomb=1 then race='ntvAm';
if raceomb=2 then race='eAsia';
if raceomb=3 then race='sAsia';
if raceomb=4 then race='natPc';
if raceomb=5 then race='black';
if raceomb=6 then race='white';
if raceomb=7 then race='blkwh';
if raceomb=8 then race='mixed';
if raceomb=9 then race='unkno';
sex = substr(sex, 1,1);
keep dPlt sex age orgn race USER_ID;run;
proc freq data=demods; tables sex; run;
/*proc print data=demoDS;run;*/
*merge with session data;
data ssds; set libB.sessiontasks1500000 libB.sessiontasks1600000; run;
/*proc print data=ssds; var session_id session_date; run;
proc freq data=ssds; tables task_id_s; run;*/
PROC SORT DATA=ssds; BY SESSION_ID; RUN;
PROC TRANSPOSE DATA=ssds OUT=tss PREFIX=s;VAR USER_ID;BY SESSION_ID; RUN;
data tss; set tss; USER_ID = s1; keep SESSION_ID USER_ID; run;
/*proc print data=tss;run;*/
PROC SORT DATA=demoDS; BY USER_ID; RUN;
PROC SORT DATA=tss; BY USER_ID; RUN;
data demoDS; merge demoDS tss; by USER_ID; drop _NAME_; run;
data demoDS; set demoDS; where SESSION_ID > 0; subject = SESSION_ID; run;

/*proc print data=demoDS;run;*/
data libB.demoDS; set demoDS;run;

/*to the merge*/
data orderDS; set libB.orderDS; run; PROC SORT DATA=orderDS; BY subject; RUN;
data demods; set libB.demods; if subject eq . then subject = session_id;run; PROC SORT DATA=demods; BY subject; RUN;
data timeds; set libB.timeds; if subject eq . then subject = session_id;run; PROC SORT DATA=timeds; BY subject; RUN;
data amp; set libb.amp; if subject eq . then subject = session_id;run;PROC SORT DATA=amp; BY subject; RUN;
data amps; set libb.amps; if subject eq . then subject = session_id;run;PROC SORT DATA=amps; BY subject; RUN;
data exp; set libb.exp; if subject eq . then subject = session_id;run; PROC SORT DATA=exp; BY subject; RUN;
data iat; set libb.iatr; if subject eq . then subject=session_id; run; PROC SORT DATA=iat; BY subject; RUN;
data iorder; set libb.iorder; if subject eq . then subject=session_id; run; PROC SORT DATA=iorder; BY subject; RUN;
data excsidr; set libb.excsidr; if subject eq . then subject=session_id; run; PROC SORT DATA=excsidr; BY subject; RUN;
data ampalt; set libb.ampalt; if subject eq . then subject=session_id; run; PROC SORT DATA=ampalt; BY subject; RUN;

data alldsd; run;
data alldsd; 
merge orderDS demods timeds amp amps exp iat excsidr iorder ampalt; 
by subject; where subject ne .; run;

proc freq data=alldsd; tables ord5; run;

proc contents data=alldsd; run;
data alldsd; set alldsd;

amp1=amp_twht1-amp_tblk1;amp2=amp_twht2-amp_tblk2;amp=amp_twht-amp_tblk;
exp = eWhite-eBlack; 

ampwht = amp_twht-amp_tgry; ampblk = amp_tblk-amp_tgry;

sdamp = std(amp_twht, amp_tblk, amp_tgry);

/*ampok = (namps > 60) and (namps<150) and sameRsp > .05 and sameRsp < .95 and read<3;*/
mamp = mean(amp_twht, amp_tblk, amp_tgry);
ampok = (namps > 60) and (namps<150) and mamp ^= 0 and mamp ^= 1 and read<3;
if ampok then do; 
	amp_ok = amp; amp1_ok = amp1; amp2_ok = amp2; 
	sdampok=sdamp; ampwhtok=ampwht; ampblkok=ampblk;
end;

if IATsidExc1=0 then iatok=iat_d;

abs_amp = abs(amp); abs_amp_ok = abs(amp_ok);

abs_tblk = abs(amp_tblk - 0.50);abs_twht = abs(amp_twht - 0.50);abs_tgry = abs(amp_tgry - 0.50);

amp_extrm = mean(abs(ampwht), abs(ampblk));

if ord3="amp20" or ord4="amp20" or ord5="amp20" or ord5="amp20" or ord6="amp20" then ampcond="2000";
else if ord3="amp75" or ord4="amp75" or ord5="amp75" or ord5="amp75" or ord6="amp75" then ampcond="100";

if intent > -2 then _intent=1; else if intent = -2 then _intent=-1;
if infl > 0 then _infl=1; else if infl < 0 then _infl=-1;
if intent<0 then __intent=intent; else if intent in (0 1 2) then __intent=3;

if infl in (1.5 2.5) then _infl_ = 1; else if infl in (-0.5 0.5) then _infl_ = 0; else if infl in (-1.5 -2.5) then _infl_ = -1;

if ampcond="100" then do; amp_ok100=amp_ok; _ampcond=0; end;
else if ampcond="2000" then do; amp_ok2000=amp_ok; _ampcond=1;end;

inflwhtblk = inflwht-inflblk; ainflwht = abs(inflwht); ainflblk = abs(inflblk);
ainflwhtblk=abs(inflwhtblk);minflwhtblk = mean(ainflwht, ainflblk); 

aeWhite=abs(eWhite-5); aeBlack=abs(eBlack-5);

aiat=abs(iatok); aexp=abs(exp); 

amp1_okXampCond = amp1_ok*_ampcond; amp_okXampCond = amp_ok*_ampcond; 
inflXamp_ok = infl*amp_ok;
inflXamp1_ok = infl*amp1_ok; 
inflXampCond = infl*_ampcond;
inflXamp1_okXAmpcond = infl*amp1_ok*_ampcond;
inflXamp_okXAmpcond = infl*amp_ok*_ampcond;

intentXamp_ok = intent*amp_ok; _intentXamp_ok = _intent*amp_ok;
intentXamp1_ok = intent*amp1_ok;
intentXampCond = intent*_ampcond;
intentXamp1_okXAmpcond = intent*amp1_ok*_ampcond;
intentXamp_okXAmpcond = intent*amp_ok*_ampcond;
inflWhtBlkXampCond = inflwhtblk*_ampcond;
_intentXamp_ok = _intent*amp_ok;
_intentXampCond=_intent*_ampCond;
_intentXamp_okXAmpcond=_intentXampCond*amp_ok;

completed = (exp ^= . and infl ^= . and intent ^= . and iat_d ^= . and amp ^= . and inflwhtblk ^=. );
if amp_ok ^= . and iatok ^= . and exp ^= . and infl ^= . and intent ^= . and inflwhtblk ^=. 
then include=1;

run;

/*FOR THE PAPER*/

/*Participants*/
proc freq data=alldsd; tables completed; run;
proc freq data=alldsd; tables mamp; where completed=1 and mamp in (0 1); run;
proc freq data=alldsd; tables read; where completed=1; run;
proc freq data=alldsd; tables iat_d; where iatok=. and completed=1; run;
proc freq data=alldsd; tables namps; where amp_ok=. and completed=1 and read<3 and mamp not in (0 1); run;
proc freq data=alldsd; tables include; where completed=1; run;
proc freq data=alldsd; tables sex; where completed=1; run;
PROC MEANS DATA=alldsd n MEAN MAXDEC=2 fw=7 std stderr;VAR age;where completed=1; run;
/*attitude measures*/
PROC MEANS DATA=alldsd n MEAN MAXDEC=2 fw=7 std stderr t prt;
VAR amp_ok exp iatok;where include=1; run;
/*general correlations*/
proc corr data=alldsd;  var amp_ok exp iatok; where include=1; run;
/*the effect of condition on priming, infl and intent*/
PROC MEANS DATA=alldsd n MEAN MAXDEC=4 fw=7 std stderr t prt;
VAR amp_ok sdamp amp_extrm abs_amp_ok infl intent;CLASS ampcond;where include=1;run;
proc ttest data=alldsd ;VAR amp_extrm sdamp infl intent;CLASS ampcond;where include=1;run;
proc corr data=alldsd;  var infl intent; with amp_extrm sdamp abs_amp_ok; where include=1; run;
/*reported awareness*/
proc freq data=alldsd; tables infl; where include=1; run;
proc corr data=alldsd;  var infl; with abs_amp_ok; where include=1; run;
proc corr data=alldsd;  with exp iatok amp2_ok; var amp_ok amp1_ok; where include=1 and infl<0; run;
proc corr data=alldsd;  with exp iatok amp2_ok; var amp_ok amp1_ok; where include=1 and infl>0; run;
/*intention*/
proc freq data=alldsd; tables intent; where include=1; run;
proc corr data=alldsd;  var intent; with abs_amp_ok; where include=1; run;
PROC MEANS DATA=alldsd n MEAN MAXDEC=2 fw=7 std stderr;VAR amp_ok abs_amp_ok amp_extrm sdamp;CLASS intent; where include=1; RUN;
PROC SORT DATA=alldsd; BY intent; RUN;
proc corr data=alldsd;  with exp iatok amp2_ok; var amp_ok amp1_ok iatok; where include=1; by intent; run;

/*specific influence*/
PROC MEANS DATA=alldsd n MEAN MAXDEC=2 fw=7 std stderr t prt;VAR inflwht inflblk;where include=1; run;
proc corr data=alldsd;
with inflwhtblk inflwht inflblk; var amp_ok amp_twht amp_tblk exp eWhite eBlack iatok; 
where include=1; run;

/**FOR REVISION, 18/JAN/2012 **/
proc corr alpha data=alldsd; var amp1 amp2; run;
proc corr alpha data=alldsd; var iat_d1 iat_d2; run;

